<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>时间</title>
</head>

<body>
    <fieldset>
        <legend>距离1970-1-1日的秒数</legend>
        <div id="id_nowendtime"></div>
        <input type="datetime-local" id="id_dtTime" /><input type="button" value="计算" onclick="getdatediffclick()" />
        <span id="id_showdifftime"></span>        
    </fieldset>
    <fieldset>
        <legend>1970-1-1后N秒的时间</legend>
        <input type="number" id="id_dtsc" /><input type="button" value="计算" onclick="addsclick()" />
        <span id="id_showstime"></span>        
    </fieldset>
    <script type="text/javascript">
    function getdatediffclick(){
        var dtTime = document.getElementById("id_dtTime");
        var showdifftime = document.getElementById("id_showdifftime");
        dttimestr = dtTime.value.replace(/T/g,' ') + ':00';
        showdifftime.innerHTML = GetDateDiff("1970-01-01 00:00:00",dttimestr,'second');
    }
    function addsclick(){
        var dtsc = document.getElementById("id_dtsc");
        var startTime = new Date('1970/01/01 00:00:00');
        var showstime = document.getElementById("id_showstime");
        showstime.innerHTML = (new Date((parseInt(dtsc.value)-3600*8)*1000)).Format("yyyy-MM-dd hh:mm:ss");
    }
function GetDateDiff(startTime, endTime, diffType) {
  //将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式 
  startTime = startTime.replace(/\-/g, '/');
  endTime = endTime.replace(/\-/g, '/');
  //将计算间隔类性字符转换为小写
  diffType = diffType.toLowerCase();
  var sTime = new Date(startTime); //开始时间
  var eTime = new Date(endTime); //结束时间
  //作为除数的数字
  var divNum = 1;
  switch (diffType) {
    case 'second':
      divNum = 1000;
      break;
    case 'minute':
      divNum = 1000 * 60;
      break;
    case 'hour':
      divNum = 1000 * 3600;
      break;
    case 'day':
      divNum = 1000 * 3600 * 24;
      break;
    default:
      break;
  }
  return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
}
Date.prototype.Format = function(fmt){
//author: meizz 
var o =
{ 
"M+" : this.getMonth() + 1, //月份 
"d+" : this.getDate(), //日 
"h+" : this.getHours(), //小时 
"m+" : this.getMinutes(), //分 
"s+" : this.getSeconds(), //秒 
"q+" : Math.floor((this.getMonth() + 3) / 3), //季度 
"S" : this.getMilliseconds() //毫秒 
}; 
if (/(y+)/.test(fmt)){ 
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); 
}
for (var k in o){ 
    if (new RegExp("(" + k + ")").test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));}
} 
return fmt; 
}


Date.prototype.addDays = function(d){
    this.setDate(this.getDate() + d);
};


Date.prototype.addWeeks = function(w){
    this.addDays(w * 7);
};


Date.prototype.addMonths= function(m){
    var d = this.getDate();
    this.setMonth(this.getMonth() + m);
    if (this.getDate() < d)
        this.setDate(0);
};


Date.prototype.addYears = function(y){
    var m = this.getMonth();
    this.setFullYear(this.getFullYear() + y);
    if (m < this.getMonth()){
        this.setDate(0);
    }
};
var now = new Date();
var nowendtime = document.getElementById("id_nowendtime");
//now.addDays(1);//加减日期操作
var tdayhtml = "";
for(i = 1;i < 11; i++){
    now.addDays(1);
    var t = now.Format("yyyy-MM-dd hh:mm:ss");
    tdayhtml += ("<p>" + t +" - "+ GetDateDiff("1970-01-01 00:00:00",t,'second') + "</p>");
}
nowendtime.innerHTML = tdayhtml;
//alert(now.Format("yyyy-MM-dd"));
//var intstr = GetDateDiff("1970-01-01 00:00:00","2016-11-15 00:00:00","second");
//alert(intstr);
 
</script>
</body>

</html>